package com.longsunhd.yum.buffalo.network.action;

import android.os.Handler;
import android.os.Message;

import com.longsunhd.yum.buffalo.base.BaseApplication;
import com.longsunhd.yum.buffalo.bean.DragListBean;
import com.longsunhd.yum.buffalo.bean.GetMedicationFieldBean;
import com.longsunhd.yum.buffalo.bean.SubmitBean;
import com.longsunhd.yum.buffalo.constant.Constant;
import com.longsunhd.yum.buffalo.network.Network;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：黄宣淇
 * 时间：2021/5/10 0010
 * 如何代码没有备注，一个月后，只有你和上帝知道--鲁迅
 */
public class MedicateApiUtils {
    private Message message=new Message();
    static MedicateApiUtils  instance;
    public static MedicateApiUtils newInstance(){

        instance = new MedicateApiUtils();

        return instance;
    }
    /*根据id获取用药详情*/
    public  void getMedicate(String token,String id,final Handler handler){
        Network.getMedicationApi(token)
                .getMedication(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<GetMedicationFieldBean>() {
                    @Override
                    public void accept(final GetMedicationFieldBean loginBean) throws Exception {
                        if(loginBean!=null){
                            message.obj=loginBean;
                            handler.sendMessage(message);
                        }else {
                            BaseApplication.showToast("获取用药信息失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.getMessage();
                        BaseApplication.showToast("获取用药信息失败");
                    }
                });
    }
    /*获取药物列表*/
    public  void getDragList(String token,final Handler handler){
        Network.getMedicationApi(token)
                .getDrugOption()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<DragListBean>() {
                    @Override
                    public void accept(final DragListBean loginBean) throws Exception {
                        if(loginBean!=null){
                            message.obj=loginBean;
                            handler.sendMessage(message);
                        }else {
                            BaseApplication.showToast("获取药物信息失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.getMessage();
                        BaseApplication.showToast("获取药物信息失败");
                    }
                });
    }
    /*编辑用药*/
    public  void postMedicate(String token,Map map,final Handler handler){
        Network.getMedicationApi(token)
                .postMedication(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<SubmitBean>() {
                    @Override
                    public void accept(final SubmitBean loginBean) throws Exception {
                        if(loginBean!=null&&loginBean.getCode()== Constant.Success){
                            message.obj=loginBean;
                            handler.sendMessage(message);
                            BaseApplication.showToast(loginBean.getMsg());
                        }else {
                            BaseApplication.showToast("提交失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.getMessage();
                        BaseApplication.showToast("提交失败");
                    }
                });
    }
}
